
package com.example.onlineshop.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.onlineshop.Domain.CartEntity;
import com.example.onlineshop.R;
import com.example.onlineshop.db.onListener;

import java.util.List;

public class CartAdapter extends RecyclerView.Adapter<CartAdapter.Viewholder> {
    List<CartEntity> items;
    Context context;
    /**
     *定义一个变量储存数据
     */
    private onListener listener;
    /**
     *提供公共的方法,并且初始化接口类型的数据
     */
    public void setListener( onListener listener){
        this.listener = listener;
    }
    public CartAdapter(List<CartEntity> items) {
        this.items = items;
    }

    @NonNull
    @Override
    public CartAdapter.Viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.viewholder_cart, parent,false);
        return new Viewholder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull CartAdapter.Viewholder holder, int position) {
        holder.titleTxt.setText(items.get(position).getTitle());
        holder.priceTxt.setText(items.get(position).getPrice()+"￥/kg");
        holder.weightTxt.setText(items.get(position).getNum()+"");

        int drawableResourceID = holder.itemView.getResources()
                .getIdentifier(items.get(position).getImgPath(),"drawable",holder.itemView.getContext().getPackageName());

        Glide.with(context)
                .load(drawableResourceID)
                .into(holder.picCart);
        /**
         * 添加点击事件
         */
        holder.plusBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener!=null){
                    //接口回調
                    listener.OnListener(1,items.get(position));
                }

            }
        });
        /**
         * 添加減少点击事件
         */
        holder.minusBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener!=null) {
                    //接口回調
                    listener.OnListener(2, items.get(position));
                }
            }
        });
        /**
         * 添加刪除点击事件
         */
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener!=null) {
                    //接口回調
                listener.OnListener(3, items.get(position));
            }

            }
        });
        holder.itemView.setOnClickListener(v -> {
//            Intent intent = new Intent(context, CartActivity.class);
//            intent.putExtra("object",items.get(position));
//            context.startActivity(intent);
        });

    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    public class Viewholder extends RecyclerView.ViewHolder {

        TextView titleTxt, priceTxt,weightTxt;
        ImageView picCart;
        TextView plusBtn, minusBtn;
       Button delete;
        public Viewholder(@NonNull View itemView) {
            super(itemView);

            titleTxt = itemView.findViewById(R.id.titleTxt);
            priceTxt = itemView.findViewById(R.id.priceTxt);
            picCart = itemView.findViewById(R.id.picCart);
            weightTxt = itemView.findViewById(R.id.weightTxt);
            minusBtn = itemView.findViewById(R.id.minuCartBtn);
            plusBtn = itemView.findViewById(R.id.plusCartBtn);
            delete = itemView.findViewById(R.id.delete);
        }
    }
}
